package com.aidong.web.controller.dic;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicSalesCodeDao;
import com.aidong.model.wills.DicSalesCode;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"dicSales"})
public class DicSalesController extends BaseController {
    @Resource
    DicSalesCodeDao dicSalesCodeDao;

    public DicSalesController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("dic/sales/list")).addObject("shopName", this.getTypeList("shopName"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "shop",required = false) String shop, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        if(!this.chkNull(status)) {
            cd.eq("status", status);
        } else {
            cd.gt("status", Integer.valueOf(0));
        }

        cd.eq("shopCode", shop);
        if(!this.chkNull(name)) {
            ConditionMap mv = new ConditionMap(1);
            mv.like("code", "%" + name + "%");
            mv.like("chName", "%" + name + "%");
            cd.addSub(mv);
        }

        this.dicSalesCodeDao.queryPageList(pg, cd, (new OrderSet()).asc("code"));
        ModelAndView mv1 = new ModelAndView("dic/sales/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        DicSalesCode sales = new DicSalesCode();
        sales.setStatus(Integer.valueOf(1));
        ModelAndView mv = new ModelAndView("dic/sales/edit");
        mv.addObject("sales", sales);
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("sales") DicSalesCode sales) {
        if(this.chkNull(sales.getCode())) {
            return this.getResult(false, "请输入编码");
        } else if(this.chkNull(sales.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNull(sales.getShopCode())) {
            return this.getResult(false, "请选择门店");
        } else {
            if(this.chkNull(sales.getStatus())) {
                sales.setStatus(Integer.valueOf(1));
            }

            if(this.dicSalesCodeDao.exists(sales.getCode())) {
                return this.getResult(false, "编码已存在");
            } else {
                sales.setCreateUser(this.getLoginUserCode());
                sales.setCreateTime(new Date());
                sales.setUpdateTime(new Date());

                try {
                    this.dicSalesCodeDao.add(sales);
                    this.reloadTypeList("salesName");
                    this.addUserLog("新增销售员", "编号:" + sales.getCode() + ",名称:" + sales.getChName());
                } catch (Exception var3) {
                    this.log.error("add dicsales", var3);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("code") String code) {
        DicSalesCode sales = this.dicSalesCodeDao.getById(code);
        if(sales == null) {
            return this.showAlert("没有这个销售员");
        } else {
            ModelAndView mv = new ModelAndView("dic/sales/edit");
            mv.addObject("sales", sales);
            mv.addObject("shopName", this.getTypeList("shopName"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("sales") DicSalesCode sales) {
        if(this.chkNull(sales.getCode())) {
            return this.getResult(false, "没有这个销售员");
        } else if(this.chkNull(sales.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNull(sales.getShopCode())) {
            return this.getResult(false, "请选择门店");
        } else {
            if(this.chkNull(sales.getStatus())) {
                sales.setStatus(Integer.valueOf(1));
            }

            DicSalesCode sales1 = this.dicSalesCodeDao.getById(sales.getCode());
            if(sales1 == null) {
                return this.getResult(false, "没有这个销售员");
            } else {
                sales1.setChName(sales.getChName());
                sales1.setShopCode(sales.getShopCode());
                sales1.setEntryDate(sales.getEntryDate());
                sales1.setPosition(sales.getPosition());
                sales1.setNote(sales.getNote());
                sales1.setStatus(sales.getStatus());
                sales1.setUpdateTime(new Date());

                try {
                    this.dicSalesCodeDao.update(sales1);
                    this.reloadTypeList("salesName");
                    this.addUserLog("修改销售员", "编号:" + sales1.getCode() + ",名称:" + sales1.getChName());
                } catch (Exception var4) {
                    this.log.error("edit dicsales", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("code") String code) {
        DicSalesCode sales = this.dicSalesCodeDao.getById(code);
        if(sales == null) {
            return this.getResult(false, "没有这个销售员");
        } else {
            try {
                this.dicSalesCodeDao.updateStatus(code, 0);
                this.reloadTypeList("salesName");
                this.addUserLog("删除销售员", "编号:" + sales.getCode() + ",名称:" + sales.getChName());
            } catch (Exception var4) {
                this.log.error("del dicsales", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}